from socket import timeout
import requests, time, json
import pandas as pd


def jd_comments(sku_id,save_path,start_page=0,end_page=5,delay=1):
    api = "https://club.jd.com/comment/productPageComments.action"
    datas = []
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
    }
    params = {
        "productId": sku_id,
        "score": 0,
        "sortType": 5,
        "page": 0,
        "pageSize": 10,
        "isShadowSku": 0,
        "rid": 0,
        "fold": 1
    }
    
    while start_page <= params["page"] < min(end_page,99):
        comments = requests.get(api, headers= headers, params= params) #发送数据请求
        print(comments.url)
        comments = json.loads(comments.text) #解析json数据
        comments = comments["comments"] #提取评论部分
        for li in range (0,10): #每页10条
            comment = comments[li]
            content = comment["content"]
            
            datas.append(content)
            print("完成{}页_{}条".format(params["page"]+1,li+1))     

        params["page"] += 1
        time.sleep(delay)
    df= pd.DataFrame(datas)
    print(df)
    file_name = r"{}/comments-{}-{}-{}.csv".format(save_path,sku_id,params["page"],li+1)
    df.to_csv(file_name)
    print(file_name)
    
    return(df)


    
if __name__ =="__main__":
    sku_li = [
        # "100006390517"
        # "1195206",
        # "927668",
        "100011453904",
        "100008765692",
        "100000903310"
    ]
    
    for sku in sku_li:
        df = jd_comments(
            sku_id = sku,
            save_path = r"/Users/angela/Downloads",
            start_page=0,
            end_page=99,
            delay=3
        )
        print(df)
        time.sleep(120)
